<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | Formsets | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20Formsets%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20Formsets%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20Formsets%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20Formsets%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/topics/forms/formsets/">go here</a>.
  
  </h2>
  <div class="section" id="s-id1">
<span id="s-formsets"></span><span id="s-topics-forms-formsets"></span><span id="id1"></span><span id="formsets"></span><span id="topics-forms-formsets"></span><h1>Formsets<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h1>
<p>A formset is a layer of abstraction to working with multiple forms on the same
page. It can be best compared to a data grid. Let’s say you have the following
form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ArticleForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">title</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
</pre></div>
</div>
<p>You might want to allow the user to create several articles at once. To create
a formset out of an <tt class="docutils literal"><span class="pre">ArticleForm</span></tt> you would do:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">from</span> <span class="nn">django.forms.formsets</span> <span class="k">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">)</span>
</pre></div>
</div>
<p>You now have created a formset named <tt class="docutils literal"><span class="pre">ArticleFormSet</span></tt>. The formset gives you
the ability to iterate over the forms in the formset and display them as you
would with a regular form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-title" id="id_form-0-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>As you can see it only displayed one form. This is because by default the
<tt class="docutils literal"><span class="pre">formset_factory</span></tt> defines one extra form. This can be controlled with the
<tt class="docutils literal"><span class="pre">extra</span></tt> parameter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mf">2</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="s-using-initial-data-with-a-formset">
<span id="using-initial-data-with-a-formset"></span><h2>Using initial data with a formset<a class="headerlink" href="#using-initial-data-with-a-formset" title="Permalink to this headline">¶</a></h2>
<p>Initial data is what drives the main usability of a formset. As shown above
you can define the number of extra forms. What this means is that you are
telling the formset how many additional forms to show in addition to the
number of forms it generates from the initial data. Lets take a look at an
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mf">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Django is now open source'</span><span class="p">,</span>
<span class="gp">... </span>     <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()},</span>
<span class="gp">... </span><span class="p">])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-title" value="Django is now open source" id="id_form-0-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-pub_date" value="2008-05-12" id="id_form-0-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-title" id="id_form-1-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-pub_date" id="id_form-1-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-title" id="id_form-2-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>There are now a total of three forms showing above. One for the initial data
that was passed in and two extra forms. Also note that we are passing in a
list of dictionaries as the initial data.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/forms/modelforms/#model-formsets"><em>Creating formsets from models with model formsets</em></a>.</p>
</div>
</div>
<div class="section" id="s-limiting-the-maximum-number-of-forms">
<span id="limiting-the-maximum-number-of-forms"></span><h2>Limiting the maximum number of forms<a class="headerlink" href="#limiting-the-maximum-number-of-forms" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">max_num</span></tt> parameter to <tt class="docutils literal"><span class="pre">formset_factory</span></tt> gives you the ability to
force the maximum number of forms the formset will display:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mf">2</span><span class="p">,</span> <span class="n">max_num</span><span class="o">=</span><span class="mf">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormset</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-title" id="id_form-0-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>A <tt class="docutils literal"><span class="pre">max_num</span></tt> value of <tt class="docutils literal"><span class="pre">0</span></tt> (the default) puts no limit on the number forms
displayed.</p>
</div>
<div class="section" id="s-formset-validation">
<span id="formset-validation"></span><h2>Formset validation<a class="headerlink" href="#formset-validation" title="Permalink to this headline">¶</a></h2>
<p>Validation with a formset is about identical to a regular <tt class="docutils literal"><span class="pre">Form</span></tt>. There is
an <tt class="docutils literal"><span class="pre">is_valid</span></tt> method on the formset to provide a convenient way to validate
each form in the formset:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">({})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
<p>We passed in no data to the formset which is resulting in a valid form. The
formset is smart enough to ignore extra forms that were not changed. If we
attempt to provide an article, but fail to do so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">'form-TOTAL_FORMS'</span><span class="p">:</span> <span class="s">u'1'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-INITIAL_FORMS'</span><span class="p">:</span> <span class="s">u'1'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-title'</span><span class="p">:</span> <span class="s">u'Test'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-pub_date'</span><span class="p">:</span> <span class="s">u''</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[{'pub_date': [u'This field is required.']}]</span>
</pre></div>
</div>
<p>As we can see the formset properly performed validation and gave us the
expected errors.</p>
<div class="section" id="s-id2">
<span id="s-understanding-the-managementform"></span><span id="id2"></span><span id="understanding-the-managementform"></span><h3>Understanding the ManagementForm<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<p>You may have noticed the additional data that was required in the formset's
data above. This data is coming from the <tt class="docutils literal"><span class="pre">ManagementForm</span></tt>. This form is
dealt with internally to the formset. If you don't use it, it will result in
an exception:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">'form-0-title'</span><span class="p">:</span> <span class="s">u'Test'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-pub_date'</span><span class="p">:</span> <span class="s">u''</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">django.forms.util.ValidationError: [u'ManagementForm data is missing or has been tampered with']</span>
</pre></div>
</div>
<p>It is used to keep track of how many form instances are being displayed. If
you are adding new forms via JavaScript, you should increment the count fields
in this form as well.</p>
</div>
<div class="section" id="s-custom-formset-validation">
<span id="custom-formset-validation"></span><h3>Custom formset validation<a class="headerlink" href="#custom-formset-validation" title="Permalink to this headline">¶</a></h3>
<p>A formset has a <tt class="docutils literal"><span class="pre">clean</span></tt> method similar to the one on a <tt class="docutils literal"><span class="pre">Form</span></tt> class. This
is where you define your own validation that deals at the formset level:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">from</span> <span class="nn">django.forms.formsets</span> <span class="k">import</span> <span class="n">BaseFormSet</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">BaseArticleFormSet</span><span class="p">(</span><span class="n">BaseFormSet</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">,</span> <span class="s">u'An error occured.'</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">formset</span><span class="o">=</span><span class="n">BaseArticleFormSet</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">({})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">non_form_errors</span><span class="p">()</span>
<span class="go">[u'An error occured.']</span>
</pre></div>
</div>
<p>The formset <tt class="docutils literal"><span class="pre">clean</span></tt> method is called after all the <tt class="docutils literal"><span class="pre">Form.clean</span></tt> methods
have been called. The errors will be found using the <tt class="docutils literal"><span class="pre">non_form_errors()</span></tt>
method on the formset.</p>
</div>
</div>
<div class="section" id="s-dealing-with-ordering-and-deletion-of-forms">
<span id="dealing-with-ordering-and-deletion-of-forms"></span><h2>Dealing with ordering and deletion of forms<a class="headerlink" href="#dealing-with-ordering-and-deletion-of-forms" title="Permalink to this headline">¶</a></h2>
<p>Common use cases with a formset is dealing with ordering and deletion of the
form instances. This has been dealt with for you. The <tt class="docutils literal"><span class="pre">formset_factory</span></tt>
provides two optional parameters <tt class="docutils literal"><span class="pre">can_order</span></tt> and <tt class="docutils literal"><span class="pre">can_delete</span></tt> that will do
the extra work of adding the extra fields and providing simpler ways of
getting to that data.</p>
<div class="section" id="s-can-order">
<span id="can-order"></span><h3><tt class="docutils literal"><span class="pre">can_order</span></tt><a class="headerlink" href="#can-order" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="xref docutils literal"><span class="pre">False</span></tt></p>
<p>Lets create a formset with the ability to order:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">can_order</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #1'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #2'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-pub_date" value="2008-05-10" id="id_form-0-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-ORDER"&gt;Order:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-ORDER" value="1" id="id_form-0-ORDER" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-ORDER"&gt;Order:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-ORDER" value="2" id="id_form-1-ORDER" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-title" id="id_form-2-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-ORDER"&gt;Order:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-ORDER" id="id_form-2-ORDER" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>This adds an additional field to each form. This new field is named <tt class="docutils literal"><span class="pre">ORDER</span></tt>
and is an <tt class="docutils literal"><span class="pre">forms.IntegerField</span></tt>. For the forms that came from the initial
data it automatically assigned them a numeric value. Lets look at what will
happen when the user changes these values:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">'form-TOTAL_FORMS'</span><span class="p">:</span> <span class="s">u'3'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-INITIAL_FORMS'</span><span class="p">:</span> <span class="s">u'2'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-title'</span><span class="p">:</span> <span class="s">u'Article #1'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-pub_date'</span><span class="p">:</span> <span class="s">u'2008-05-10'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-ORDER'</span><span class="p">:</span> <span class="s">u'2'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-1-title'</span><span class="p">:</span> <span class="s">u'Article #2'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-1-pub_date'</span><span class="p">:</span> <span class="s">u'2008-05-11'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-1-ORDER'</span><span class="p">:</span> <span class="s">u'1'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-2-title'</span><span class="p">:</span> <span class="s">u'Article #3'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-2-pub_date'</span><span class="p">:</span> <span class="s">u'2008-05-01'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-2-ORDER'</span><span class="p">:</span> <span class="s">u'0'</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #1'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #2'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">ordered_forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span>
<span class="go">{'pub_date': datetime.date(2008, 5, 1), 'ORDER': 0, 'title': u'Article #3'}</span>
<span class="go">{'pub_date': datetime.date(2008, 5, 11), 'ORDER': 1, 'title': u'Article #2'}</span>
<span class="go">{'pub_date': datetime.date(2008, 5, 10), 'ORDER': 2, 'title': u'Article #1'}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-can-delete">
<span id="can-delete"></span><h3><tt class="docutils literal"><span class="pre">can_delete</span></tt><a class="headerlink" href="#can-delete" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="xref docutils literal"><span class="pre">False</span></tt></p>
<p>Lets create a formset with the ability to delete:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">can_delete</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #1'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #2'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="go">....    print form.as_table()</span>
<span class="go">&lt;input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" /&gt;&lt;input type="hidden" name="form-INITIAL_FORMS" value="2" id="id_form-INITIAL_FORMS" /&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-pub_date" value="2008-05-10" id="id_form-0-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-DELETE"&gt;Delete:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-1-DELETE"&gt;Delete:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-title" id="id_form-2-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-2-DELETE"&gt;Delete:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>Similar to <tt class="docutils literal"><span class="pre">can_order</span></tt> this adds a new field to each form named <tt class="docutils literal"><span class="pre">DELETE</span></tt>
and is a <tt class="docutils literal"><span class="pre">forms.BooleanField</span></tt>. When data comes through marking any of the
delete fields you can access them with <tt class="docutils literal"><span class="pre">deleted_forms</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">'form-TOTAL_FORMS'</span><span class="p">:</span> <span class="s">u'3'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-INITIAL_FORMS'</span><span class="p">:</span> <span class="s">u'2'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-title'</span><span class="p">:</span> <span class="s">u'Article #1'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-pub_date'</span><span class="p">:</span> <span class="s">u'2008-05-10'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-0-DELETE'</span><span class="p">:</span> <span class="s">u'on'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-1-title'</span><span class="p">:</span> <span class="s">u'Article #2'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-1-pub_date'</span><span class="p">:</span> <span class="s">u'2008-05-11'</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-1-DELETE'</span><span class="p">:</span> <span class="s">u''</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-2-title'</span><span class="p">:</span> <span class="s">u''</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-2-pub_date'</span><span class="p">:</span> <span class="s">u''</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">'form-2-DELETE'</span><span class="p">:</span> <span class="s">u''</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #1'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="s">u'Article #2'</span><span class="p">,</span> <span class="s">'pub_date'</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mf">2008</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">deleted_forms</span><span class="p">]</span>
<span class="go">[{'DELETE': True, 'pub_date': datetime.date(2008, 5, 10), 'title': u'Article #1'}]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-adding-additional-fields-to-a-formset">
<span id="adding-additional-fields-to-a-formset"></span><h2>Adding additional fields to a formset<a class="headerlink" href="#adding-additional-fields-to-a-formset" title="Permalink to this headline">¶</a></h2>
<p>If you need to add additional fields to the formset this can be easily
accomplished. The formset base class provides an <tt class="docutils literal"><span class="pre">add_fields</span></tt> method. You
can simply override this method to add your own fields or even redefine the
default fields/attributes of the order and deletion fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">BaseArticleFormSet</span><span class="p">(</span><span class="n">BaseFormSet</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">add_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">BaseArticleFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_fields</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>
<span class="gp">... </span>        <span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s">"my_field"</span><span class="p">]</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">formset</span><span class="o">=</span><span class="n">BaseArticleFormSet</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">()</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-title"&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-title" id="id_form-0-title" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-pub_date"&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for="id_form-0-my_field"&gt;My field:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="form-0-my_field" id="id_form-0-my_field" /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-using-a-formset-in-views-and-templates">
<span id="using-a-formset-in-views-and-templates"></span><h2>Using a formset in views and templates<a class="headerlink" href="#using-a-formset-in-views-and-templates" title="Permalink to this headline">¶</a></h2>
<p>Using a formset inside a view is as easy as using a regular <tt class="docutils literal"><span class="pre">Form</span></tt> class.
The only thing you will want to be aware of is making sure to use the
management form inside the template. Let's look at a sample view:</p>
<div class="highlight-python"><pre>def manage_articles(request):
    ArticleFormSet = formset_factory(ArticleForm)
    if request.method == 'POST':
        formset = ArticleFormSet(request.POST, request.FILES)
        if formset.is_valid():
            # do something with the formset.cleaned_data
    else:
        formset = ArticleFormSet()
    return render_to_response('manage_articles.html', {'formset': formset})</pre>
</div>
<p>The <tt class="docutils literal"><span class="pre">manage_articles.html</span></tt> template might look like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">method=</span><span class="s">"POST"</span> <span class="na">action=</span><span class="s">""</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">formset.management_form</span> <span class="cp">}}</span>
    <span class="nt">&lt;table&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">form</span> <span class="k">in</span> <span class="nv">formset.forms</span> <span class="cp">%}</span>
        <span class="cp">{{</span> <span class="nv">form</span> <span class="cp">}}</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="nt">&lt;/table&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>However the above can be slightly shortcutted and let the formset itself deal
with the management form:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">method=</span><span class="s">"POST"</span> <span class="na">action=</span><span class="s">""</span><span class="nt">&gt;</span>
    <span class="nt">&lt;table&gt;</span>
        <span class="cp">{{</span> <span class="nv">formset</span> <span class="cp">}}</span>
    <span class="nt">&lt;/table&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>The above ends up calling the <tt class="docutils literal"><span class="pre">as_table</span></tt> method on the formset class.</p>
<div class="section" id="s-using-more-than-one-formset-in-a-view">
<span id="using-more-than-one-formset-in-a-view"></span><h3>Using more than one formset in a view<a class="headerlink" href="#using-more-than-one-formset-in-a-view" title="Permalink to this headline">¶</a></h3>
<p>You are able to use more than one formset in a view if you like. Formsets
borrow much of its behavior from forms. With that said you are able to use
<tt class="docutils literal"><span class="pre">prefix</span></tt> to prefix formset form field names with a given value to allow
more than one formset to be sent to a view without name clashing. Lets take
a look at how this might be accomplished:</p>
<div class="highlight-python"><pre>def manage_articles(request):
    ArticleFormSet = formset_factory(ArticleForm)
    BookFormSet = formset_factory(BookForm)
    if request.method == 'POST':
        article_formset = ArticleFormSet(request.POST, request.FILES, prefix='articles')
        book_formset = BookFormSet(request.POST, request.FILES, prefix='books')
        if article_formset.is_valid() and book_formset.is_valid():
            # do something with the cleaned_data on the formsets.
    else:
        article_formset = ArticleFormSet(prefix='articles')
        book_formset = BookFormSet(prefix='books')
    return render_to_response('manage_articles.html', {
        'article_formset': article_formset,
        'book_formset': book_formset,
    })</pre>
</div>
<p>You would then render the formsets as normal. It is important to point out
that you need to pass <tt class="docutils literal"><span class="pre">prefix</span></tt> on both the POST and non-POST cases so that
it is rendered and processed correctly.</p>
</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">Formsets</a><ul>
<li><a class="reference external" href="#using-initial-data-with-a-formset">Using initial data with a formset</a></li>
<li><a class="reference external" href="#limiting-the-maximum-number-of-forms">Limiting the maximum number of forms</a></li>
<li><a class="reference external" href="#formset-validation">Formset validation</a><ul>
<li><a class="reference external" href="#id2">Understanding the ManagementForm</a></li>
<li><a class="reference external" href="#custom-formset-validation">Custom formset validation</a></li>
</ul>
</li>
<li><a class="reference external" href="#dealing-with-ordering-and-deletion-of-forms">Dealing with ordering and deletion of forms</a><ul>
<li><a class="reference external" href="#can-order"><tt class="docutils literal"><span class="pre">can_order</span></tt></a></li>
<li><a class="reference external" href="#can-delete"><tt class="docutils literal"><span class="pre">can_delete</span></tt></a></li>
</ul>
</li>
<li><a class="reference external" href="#adding-additional-fields-to-a-formset">Adding additional fields to a formset</a></li>
<li><a class="reference external" href="#using-a-formset-in-views-and-templates">Using a formset in views and templates</a><ul>
<li><a class="reference external" href="#using-more-than-one-formset-in-a-view">Using more than one formset in a view</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20Formsets%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/topics/forms/modelforms/">Creating forms from models</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/topics/forms/media/">Form Media</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/">Using Django</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/forms/">Working with forms</a>
          
          <ul><li>Formsets</li></ul>
          </li></ul></li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20Formsets%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>